<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="user_withdrawal")
 */
class Withdrawal extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\UserWithdrawal();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            foreach($where as $key =>&$val){
                if($val[0] == 'state'){
                    $val[0] = 'a.state';
                }
            }
            
            $admin = session('admin');//
            $param = [];
            if(!empty($admin['uid'])){
                $param['b.bucket_id'] = $admin['uid'];
            }

            $count = $this->model
                ->alias('a')
                ->join("user b","a.uid = b.id")
                ->where($param)
                ->where($where)
                ->count();
            $list = $this->model
                ->alias('a')
                ->join("user b","a.uid = b.id")
                ->where($param)
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->field("a.*, b.nickname")
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    public function verify($id){
        $row = $this->model
        ->alias('a')
        ->join("user b","a.uid = b.id")
        ->where("a.id=$id")
        ->field("a.*,b.nickname,b.payee_name,b.payee_bankno,payee_bankname,b.phone")
        ->find();
       
        empty($row) && $this->error('数据不存在');
        
        if ($this->request->isPost()) {
            //判断是否为可操作状态
            if($row['state'] != 0){
                $this->error("申请已被处理！");
            }
                
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                // var_dump($post);die;
                $update = $this->model->where(['id' => $id])->update(['state' => $post['state'], 'msg' => $post['msg']]);
               
                // var_dump($income);die;
                if($post['state'] == 1){
                    Db::name("user_income")->where(['id' => $row['uid']])->inc('withdrawal',$post['amount'])->update();
                } elseif($post['state'] == "-1"){
                  
                    Db::name("user_income")->where(['id' => $row['uid']])->inc('income',$post['amount'])->update();
                }
            } catch (\Exception $e) {
                //echo Db::name()->getLastSql();die;
                $this->error('保存失败');
            }
            $update ? $this->success('保存成功') : $this->error('保存失败');
        }

        $this->assign('row', $row);
        return $this->fetch();
    }
    
  
}